﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace EntityModule
{    /// <summary>
    /// tan.do string extension
    /// </summary>
    public static class stringExtension
    {
        public static Guid AsGuid(this object o)
        {
            Guid guid = Guid.Empty;
            if (o == null) return guid;
            Guid.TryParse(o.ToString(), out guid);
            return guid;
        }

        public static int AsInt(this string o)
        {
            if (o==null) return 0;
            int obj = 0;
            if (o is double || o is decimal)
                return (int)Math.Round(o.AsDouble(), 0);
            int.TryParse(o.ToString(), out obj);
            return obj;
        }
        public static double AsNumberSolar(this double o)
        {
            return o < 0 ? 0 : o;
        }
        public static string AsString(this object o)
        {
            if (o == null) return string.Empty;
            return o.ToString();
        }
        public static string AsDateString(this DateTime o)
        {
            return o.ToString("dd/MM/yyyy");
        }
        public static string AsDateString(this string o)
        {
            return o.AsString().AsDate().ToString("dd/MM/yyyy");
        }
        public static string AsHourMinute(this DateTime o)
        {
            return o.ToString("HH:mm");
        }
        public static List<string> AsListString(this string o)
        {
            var lis = new List<string>();
            lis.Add(o.ToString());
            return lis;
        }
        public static string AsListString(this List<Guid> o)
        {
            string result = string.Empty;
            foreach (Guid guid in o)
            {
                result = guid + ",";
            }
            return result;
        }
        public static string AsArray(this List<string> list)
        {
            string result = string.Empty;
            int i = 0;
            foreach (string s in list)
            {
                if (i == 0)
                    result += s;
                else result += ", " + s;
                i++;
            }
            return result;
        }
        public static string AsArray(this List<Guid> list)
        {
            string result = string.Empty;
            int i = 0;
            foreach (Guid s in list)
            {
                if (i == 0)
                    result += s;
                else result += ", " + s;
                i++;
            }
            return result;
        }
        public static List<Guid> AsListGuid(this string o)
        {
            var lis = new List<Guid>();
            lis.Add(o.AsGuid());
            return lis;
        }
        public static List<Guid> AsListGuid(this string[] strings)
        {
            strings = strings.Distinct().ToArray();
            var lis = new List<Guid>();
            foreach (var guid in strings)
            {
                lis.Add(guid.AsGuid());
            }
            return lis;
        }
        public static string[] AsColumnArray(this DataTable dataTable)
        {
            var lstColumns = new List<string>();
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                string colName = dataTable.Columns[i].ColumnName;
                lstColumns.Add(colName);
            }
            return lstColumns.ToArray();
        }
        public static string[] AsColumnArrayNotID(this DataTable dataTable)
        {
            var lstColumns = new List<string>();
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                string colName = dataTable.Columns[i].ColumnName;
                if (colName.Substring(0, 2).ToUpper() != "ID")
                    lstColumns.Add(colName);
            }
            return lstColumns.ToArray();
        }
        /// <summary>
        /// Convert text to date time
        /// </summary>
        /// <param name="o"></param>
        /// <returns>Date time format vi-Vn</returns>
        public static DateTime AsDate(this string o)
        {
            DateTime date = new DateTime(1900, 1, 1);
            CultureInfo info = new CultureInfo("vi-VN");
            try
            {
                if (!DateTime.TryParse(o.AsString(), info, DateTimeStyles.None, out date))
                {
                    string str = o.ToString().Replace("/", "");
                    if (str.Length == 6)
                        return new DateTime(str.Substring(2, 4).AsInt(), str.Substring(0, 2).AsInt(), 1);
                    return new DateTime(str.Substring(4, 4).AsInt(), str.Substring(2, 2).AsInt(), str.Substring(0, 2).AsInt());
                }
            }
            catch
            {
                return new DateTime(1900, 1, 1);
            }
            return date;
        }
        public static double AsDouble(this string o)
        {
            if (o==null) return 0;
            double temp = 0;
            double.TryParse(o.ToString(), out temp);
            return temp;
        }
        public static bool AsBool(this string o)
        {
            if (o.IsNull()) return false;
            bool temp = false;
            bool.TryParse(o.ToString(), out temp);
            return temp;
        }
        public static string AsDoubleString(this string o)
        {
            if (o.IsNull()) return string.Empty;
            double temp = 0;
            double.TryParse(o.ToString(), out temp);
            return temp.ToString("###,###.##");
        }

        public static int Abs(this int o)
        {
            return Math.Abs(o);
        }
        public static bool IsNotNullOrEmpty(this object o)
        {
            if (o == null) return false;
            return !string.IsNullOrEmpty(o.ToString());
        }
        public static bool IsNullOrEmpty(this string o)
        {
            if (o == null) return true;
            return string.IsNullOrEmpty(o.ToString());
        }
        public static bool IsNull(this string o)
        {
            if (o == null) return true;
            return false;
        }
        public static bool IsNotNull(this string o)
        {
            if (o == null) return false;
            return true;
        }
        public static bool IsGuiEmpty(this object o)
        {
            if (o.AsGuid() == Guid.Empty) return true;
            return false;
        }
        public static DataTable Sort(this DataTable table, string[] strings)
        {
            string columSort = string.Empty;
            foreach (string s in strings)
            {
                if (table.Columns.Contains(s))
                {
                    columSort = s;
                    break;
                }
            }
            if (columSort.IsNullOrEmpty() || table.Rows.Count == 0) return table;
            DataView dataView = table.DefaultView;
            dataView.Sort = columSort;
            return dataView.ToTable();
        }
        /*  public static T Clone<T>(this T source)
          {
              var obj = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
              using (var stream = new System.IO.MemoryStream())
              {
                  obj.Writestring(stream, source);
                  stream.Seek(0, System.IO.SeekOrigin.Begin);
                  return (T)obj.Readstring(stream);
              }
          }*/
        public static DataTable AsDataTable(this string o, string[] arryColumn)
        {
            DataTable table = new DataTable();
            var dc = new DataColumn[] { new DataColumn("ID") };
            table.Columns.AddRange(dc);
            table.PrimaryKey = dc;
            foreach (string s in arryColumn)
            {
                if (s.IndexOf("Date") > -1)
                    table.Columns.Add(s, typeof(DateTime));
                else table.Columns.Add(s);
            }
            return table;
        }
        public static string GetSpecial(this string s)
        {
            string result = string.Empty;
            char[] list = s.ToUpper().ToArray();
            foreach (char s1 in list)
            {
                result += "[" + s1 + "]";
            }
            return result;
        }
        public static string GetSpecial(this DateTime? date)
        {
            if (date == null) return "";
            return GetSpecial(date.Value.ToString("ddMMyyyy"));
        }
        public static string GetStrDate(this DateTime? date)
        {
            if (date == null) return " Ngày tháng năm";
            DateTime dateTime = date.Value;
            return "ngày " + dateTime.Day + " tháng " + dateTime.Month + " năm " + dateTime.Year;
        }
        public static string GetStrDate(this DateTime date)
        {
            DateTime dateTime = date;
            return "ngày " + dateTime.Day + " tháng " + dateTime.Month + " năm " + dateTime.Year;
        }
    }
}
